from bs4 import BeautifulSoup


def create_html(json_data: dict) -> str:
    """bs4 创建商品信息HTML片段

    Args:
        json_data (dict)

    Returns:
        str: HTML文本
    """
    # 字段翻译中文
    translate_cn = {
        "item_cn_name": "产品中文名",
        "item_cn_alia": "产品中文别名",
        "item_en_name": "产品英文名",
        "item_specification": "纯度",
        "item_color": "颜色",
        "chem_cas": "CAS 号",
        "item_storage": "存储条件",
        "chem_mw": "分子量",
        "item_melting": "熔点",
        "item_boiling": "沸点",
        "item_flash": "闪点",
        "item_density": "密度",
        "item_refractive": "折光率",
        "chem_einecs": "EINECS编号",
    }

    allow_keys = list(translate_cn.keys())

    # 创建一个空的 BeautifulSoup 对象
    soup = BeautifulSoup("", "html.parser")

    # 创建一个 <style> 标签并添加 CSS 样式
    style_tag = soup.new_tag("style")
    # style样式
    style_content = """
    .cxsw table{width:100%;border-collapse:collapse;font-family:Arial,sans-serif}.cxsw td{padding:12px;text-align:left;vertical-align:top;border:1px solid #ddd;background-color:#fff;transition:background-color .3s ease}.cxsw tr>td:first-child span{font-weight:600}.cxsw tbody tr:hover td{background-color:#f1f1f1}.cxsw span{display:block;word-wrap:break-word;word-break:break-all;overflow:hidden;text-overflow:ellipsis;line-height:1.5;color:#333}.cxsw tbody tr:nth-child(even){background-color:#f2f2f2}.cxsw thead tr td span{text-align:center;color:#409eff;font-size:24px}
    """

    style_tag.string = style_content
    soup.append(style_tag)

    # 创建div
    div_tag = soup.new_tag("div")
    div_tag.attrs["class"] = "cxsw"
    # 创建 <table> 结构
    table_tag = soup.new_tag("table")

    # 辰星生物
    thead_tag = soup.new_tag("thead")
    thead_tr_tag = soup.new_tag("tr")
    head_td_tag = soup.new_tag("td", colspan="2")  # 确保标题占据整行
    head_span_tag = soup.new_tag("span")
    head_span_tag.string = "辰星生物"

    head_td_tag.append(head_span_tag)
    thead_tr_tag.append(head_td_tag)
    thead_tag.append(thead_tr_tag)

    tbody_tag = soup.new_tag("tbody")

    # 遍历字典，创建表格行
    for key in allow_keys:
        # get 返回指定键的值 如果不存在设置默认值为空
        value = json_data.get(key, '').strip()  # 使用 get 方法并 strip 去除空格
        if value:
            tr_tag = soup.new_tag("tr")

            # 创建第一列（键或描述）
            desc_td_tag = soup.new_tag("td")
            desc_span_tag = soup.new_tag("span")
            desc_span_tag.string = translate_cn.get(key, key)  # 使用翻译字典获取中文描述
            desc_td_tag.append(desc_span_tag)

            # 创建第二列（值）
            value_td_tag = soup.new_tag("td")
            value_span_tag = soup.new_tag("span")
            value_span_tag.string = value
            value_td_tag.append(value_span_tag)

            # 将两个单元格添加到行中
            tr_tag.extend([desc_td_tag, value_td_tag])
            tbody_tag.append(tr_tag)

    table_tag.extend([thead_tag, tbody_tag])
    div_tag.append(table_tag)

    soup.append(div_tag)

    image_base64 = ""

    for _ in range(10):
        img_tag = soup.new_tag("img")
        img_tag["src"] = image_base64
        soup.append(img_tag)

    # 返回生成的HTML
    return str(soup)


def create_transparant_img():
    soup = BeautifulSoup("", "html.parser")
    image_base64 = ""

    for _ in range(10):
        img_tag = soup.new_tag("img")
        img_tag["src"] = image_base64
        soup.append(img_tag)
        # 返回生成的HTML
    return str(soup)


def advertising_html():
    return """
    <div id="con_header"style="display: flex;"><a href="https://gzcxsw.top/#/user/gift"title="点击进入积分商城"target="_blank"><img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/b8a6ee5cd5e9b4ceb809b9f47898dbe.png"height="120"width="400"></a><a href="https://work.weixin.qq.com/kfid/kfcd1ffef164b328abc"title="点击在线客服咨询"target="_blank"><img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/d319a18f235ab79d6133e8b3d0382fa.png"height="120"width="400"></a></div>
    """
